home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / Simula4.07 / Simula 4.07ƒ / SInterfaces / macProcessMgr.sim < prev    next >
Encoding:
Text File  |  1989-05-01  |  8.7 KB  |  274 lines  |  [TEXT/MPS ]

  1. % ---------------------------------------------------------------------------
  2. %    Class MACProcessMGR
  3. %
  4. % Part of the Lund Software interface to Macintosh Toolbox
  5. % MACProcessMGR is part of the programming interface. 
  6. % Processes are used to capture events that are directed to
  7. % a window. The MACProcessMGR are receiving the events from the
  8. % Mac and distributes them to the concerned window.
  9. % For each Window - create also a MACProcess and hook it to the window
  10. % by calling RegisterProcess.
  11. % 890406/Boris Magnusson
  12. % 890429/Boris Magnusson Made registerd Window selected 
  13. % ---------------------------------------------------------------------------
  14. External class macPoint="::SInterfaces:macPoint";
  15. External class macRect="::SInterfaces:macRect";
  16. External class macEvent="::SInterfaces:macEvent";
  17. External class MacProcess="::SInterfaces:MacProcess";
  18. External class MacWindowMgr="::SInterfaces:MacWindowMgr";
  19. External class MacEventMgr="::SInterfaces:MacEventMgr";
  20. External class macToolConst="::SInterfaces:macToolConst";
  21. External class macUtilities="::SInterfaces:macUtilities";
  22. MacWindowMgr class MacProcessMGR;
  23. begin
  24.     ref(macToolConst) TConst; ! Shared by each Process ;
  25.     ref(macUtilities) Util;   ! also shared by processes ;
  26.     ref(MacEventMgr) EventMgr;
  27.     Boolean terminate;        ! set to true by 'stop' to make 'Run' quit. ;
  28.  
  29. % -- system xxx moved from  toolboxDesk -- used only from processMGR -- 
  30. % -- copied also to macProcessMgr ------
  31. %    PROCEDURE SystemClick(theEvent: EventRecord; theWindow: WindowPtr);
  32.     PROCEDURE SystemClick(theEvent, theWindow);
  33.         ref(MacEvent) theEvent;
  34.         ref(MACWindow) theWindow;
  35.             EventMgr.ToolboxSystemClick(theEvent.what, theWindow.WindowPtr);
  36.  
  37. %    PROCEDURE SystemTask;
  38.     PROCEDURE SystemTask;
  39.         EventMgr.ToolboxSystemTask;
  40. % ------------------------------------------
  41.  
  42. ! Return reference to the process controlling the Active window. ;
  43.     ref(macProcess) procedure CurrentProcess;
  44.     begin
  45.         currentprocess:- if ActivePN=/=None then ActivePN.P
  46.             else none;
  47.     end;
  48.     
  49. ! Register a Process as controlling  a Window ;
  50.     procedure RegisterProcess(P,W); ref(MacProcess) P; ref(MacWindow) W;
  51.     begin
  52.         ActivePN:-new ProcessNotice(P,W);
  53.         P.Util:-Util;
  54.         P.TConst:-TConst;
  55.         SelectWindow(ActivePN.W);
  56.         call(ActivePN.P); ! -- run initialization code of Process, ;
  57.                           ! -- up to first getnextEvent. ;
  58.         ActivePN.into(EventWaitQ);    
  59.     end--- Controlling Window ---;
  60.     
  61. ! register a Process as controlling the Menu bar;    
  62.     procedure RegisterMenuProcess(p); ref(MacProcess) P;
  63.     begin
  64.         MenuController:-P;
  65.         P.Util:-Util;
  66.         P.TConst:-TConst;
  67.         call(MenuController); ! perform inital actions ;
  68.     end;
  69.  
  70. ! register a Process as controlling the System Window;    
  71.     procedure RegisterSystemProcess(p); ref(MacProcess) P;
  72.     begin
  73.         SystemController:-P;
  74.         P.Util:-Util;
  75.         P.TConst:-TConst;
  76.         call(SystemController); ! perform inital actions;
  77.     end;
  78.  
  79. ! remove a process from the system ;
  80.     procedure Kill(toKill); ref(macProcess) toKill;
  81.     begin
  82.         ref(processNotice) PN;
  83.         PN:-convertPtoPN(toKill);
  84.         PN.out;
  85.     end -- kill -- ;
  86.     
  87. ! Make 'RUN' Terminate next time somebody leaves control ;    
  88.     Procedure Stop;
  89.         Terminate:=true;
  90.  
  91. ! - Get events and dispatch to controler of 'hit' window. -    ;    
  92.     Procedure Run;
  93.     begin
  94.         Boolean Gotten_an_Event,Inside;
  95.         integer code; ! Hit region encoding ;
  96.         integer v_h,height,width;
  97.         ref(MacPoint) ThePoint;
  98.         ref(MacEvent) TheEvent;
  99.         ref(MacWindow) TheWindow;
  100.         ref(ProcessNotice) PN;
  101.         ref(macRect) ScreenBounds;
  102.         Terminate:=false;
  103.         TheEvent:- new MacEvent;
  104.         ThePoint:- new MacPoint;
  105.         ScreenBounds:-new MACrect;
  106.         getScreenRect(ScreenBounds);
  107.         while not Terminate do
  108.         begin
  109.             Gotten_an_Event:=EventMgr.getnextEvent(-1,theEvent); ! all events ;
  110.             if Gotten_an_Event then
  111.             begin
  112.                 if theEvent.what=TConst.mousedown then
  113.                 begin
  114.                     thePoint.v:=theEvent.point_v;
  115.                     thePoint.h:=theEvent.point_h;
  116.                     code:=FindWindow(thePoint,theWindow);
  117.                     if code =Tconst.inSysWindow then
  118.                     begin
  119.                         if systemController=/=none and then
  120.                             util.bitand(Util.BitShift(1,theEvent.what) ,
  121.                                 systemController.CurrentMask )<>0 then
  122.                         begin
  123.                             SystemController.CurrentEvent:-theEvent;
  124.                             Call(SystemController);
  125.                         end
  126.                         else
  127.                         begin
  128.                             Systemclick(TheEvent,TheWindow);
  129.                         end -- system - click -- ;
  130.                     end -- in sys Window --
  131.                     else if code =Tconst.inMenuBar then
  132.                     begin
  133.                         if MenuController =/= none and then 
  134.                             util.bitand(Util.BitShift(1,theEvent.what) ,
  135.                             Menucontroller.CurrentMask )<>0 then
  136.                         begin
  137.                             MenuController.CurrentEvent:-theEvent;
  138.                             MenuController.Currentfindresult:=code;
  139.                             Call(MenuController);
  140.                         end;
  141.                     end - in menu bar -
  142.                     else
  143.                     begin ! -- mouse down in user window --;
  144.                         PN:-ConvertToPN(theWindow);        
  145.                         if PN==none then
  146.                             ! Ignore event for some unknown Window ;
  147.                         else if PN=/=ActivePN then
  148.                         begin
  149.                             ActivePN:-PN;                ! - Notice of current Window.;
  150.                             SelectWindow(PN.W);     ! - First hit just selects - ;
  151.                             if Code=Tconst.inDrag then ! - and possily drags ! ;
  152.                                 PN.W.DragWindow(thePoint,ScreenBounds);
  153.                         end -- first mused-down activates -
  154.                         else if Code=Tconst.inDrag then
  155.                             PN.W.DragWindow(thePoint,ScreenBounds)
  156.                         else if Code=Tconst.inGrow then
  157.                         begin
  158.                             v_h:=PN.W.GrowWindow(thePoint,ScreenBounds);
  159.                             height:=util.hiword(v_h);width:=util.loword(v_h);
  160.                             PN.P.doSize(width,height);
  161.                         end -- User Window re-size --
  162.                         else if Code=Tconst.ingoAway then
  163.                         begin
  164.                             inside:=PN.W.TrackGoAway(thePoint);
  165.                             if inside then PN.P.doGoAway;
  166.                         end -- User Window go-away --
  167.                         else if Util.bitand(Util.BitShift(1,theEvent.what) ,
  168.                                 PN.P.CurrentMask  )<>0 then
  169.                             PN.into(ReadyQ) ! MouseDown event for user Window ;
  170.                         else
  171.                         ! Discard Disabled Mouse down Event ; ;
  172.                     end -- Muse down in user Window -- ;
  173.                 end -- Mouse-down Event --
  174.                 else if theEvent.what=TConst.keyDown and  then
  175.                     util.bitAnd(theEvent.modifiers,TConst.cmdKey)<>0 then
  176.                 begin
  177.                     MenuController.CurrentEvent:-theEvent;
  178.                     MenuController.Currentfindresult:=code;
  179.                     Call(MenuController);
  180.                     !!! if no hit then ...;
  181.                 end - Meta key -                     
  182.                 else if theEvent.what=TConst.updateEvt or else
  183.                     theEvent.what=TConst.ActivateEvt then
  184.                 begin
  185.                     theWindow:-ConvertToWindow(theEvent.Message);
  186.                     PN:-ConvertToPN(theWindow);        
  187.                     if PN==none then
  188.                     begin
  189.                         ! Ignore event for some unknown Window ;
  190.                     end
  191.                     else if Util.bitand(Util.BitShift(1,theEvent.what) ,
  192.                         PN.P.CurrentMask  )<>0 then
  193.                     begin
  194.                         PN.W.Setport; ! Temporarily make updated Window ;
  195.                                       ! the port for Quickdraw ;
  196.                         PN.P.CurrentEvent:-theEvent;
  197.                         PN.P.CurrentFindResult:=Code;
  198.                         PN.W qua macQuickdraw.TRAP
  199.                             .ToolboxGlobaltoLocal(theEvent.point_v);
  200.                         Call(PN.P);
  201.                         if PN.P.Terminate then
  202.                             PN.out;
  203.                         FrontWindow.Setport; ! revert to Active Window ;
  204.                     end;
  205.                 end -- Upate/Activate event --
  206.                 else
  207.                 Begin ! --- all other events go to Previous window ;
  208.                      if ActivePN=/=none and then
  209.                         Util.bitand(Util.BitShift(1,theEvent.what) ,
  210.                         ActivePN.P.CurrentMask  )<>0 then
  211.                         ActivePN.into(ReadyQ);
  212.                 End - other event for Old Window -;
  213.             end - gotten an event --
  214.             else
  215.             begin ! -- send NULL event to active process if enabled -- ;
  216.                 if ActivePN=/=none and then
  217.                     Util.bitand(Util.BitShift(1,Tconst.NullEvent) ,
  218.                     ActivePN.P.CurrentMask  )<>0 then
  219.                 begin
  220.                     ActivePN.into(ReadyQ);
  221.                     theEvent.what:=Tconst.NullEvent;
  222.                 end;
  223.             end - null event -- ;
  224.             while not readyQ.Empty do ! currently at most one in ReadyQ;
  225.             begin
  226.                 PN:-ReadyQ.first;
  227.                 PN.out;
  228.                 PN.P.CurrentEvent:-theEvent;
  229.                 PN.P.CurrentFindResult:=Code;
  230.                 PN.W qua macQuickdraw.TRAP
  231.                     .ToolboxGlobaltoLocal(theEvent.point_v);
  232.                 Call(PN.P);
  233.                 if not PN.P.Terminate then
  234.                     PN.into(EventWaitQ);
  235.             end - run the process of known window - ;
  236.         end - while - ;
  237.     end --- Run --- ;
  238.     
  239.     ! -------------- Local routines ------------ ;
  240.     Link class ProcessNotice(P,W); ref(MacProcess) p; ref(MacWindow) W;;
  241.     
  242.     ref(Head) ReadyQ,EventWaitQ;
  243.     ref(MacProcess) MenuController, SystemController;
  244.     ref(ProcessNotice) ActivePN;
  245.     
  246.     ref(ProcessNotice) procedure ConvertToPN(aWindow);
  247.         ref(MACWindow) aWindow;
  248.     begin
  249.         ref(ProcessNotice) pPN;
  250.         pPN:-EventWaitQ.first;
  251.         while pPN=/=none and then pPN.W=/=aWindow do
  252.             pPN:-pPN.suc;
  253.         ConvertToPn:-pPN;
  254.     end -- ConvertToPN --;
  255.     
  256.     ref(ProcessNotice) procedure ConvertPToPN(aP);
  257.         ref(MACprocess) aP;
  258.     begin
  259.         ref(ProcessNotice) pPN;
  260.         pPN:-EventWaitQ.first;
  261.         while pPN=/=none and then pPN.P=/=aP do
  262.             pPN:-pPN.suc;
  263.         ConvertPToPn:-pPN;
  264.     end -- ConvertToPN --;
  265.     
  266.     ReadyQ :- new Head;
  267.     EventWaitQ:- new Head;
  268.     EventMgr:-new MacEventMgr;
  269.     TConst:-new macToolConst;
  270.     Util:- new macUtilities;
  271.     SystemController:-none; ! :- new DefaultSystemController;
  272. end --- Process MGR --- ;
  273.